<?php

namespace app\controller;

use app\BaseController;
use app\model\CodStatement;
use app\services\CodStatementService;
use app\services\MergeService;
use app\utils\ExcelUtils;
use app\validate\FileValidate;
use app\validate\OrderValidate;
use think\App;

class HotelKingController extends BaseController
{


    /**
     * @var CodStatementService
     */
    private $service;

    public function __construct(App $app)
    {
        $this->service = new CodStatementService();
        parent::__construct($app);
    }
    
    
    public function merge(){
        $uploadedFile = $this->request->file('excel');


        validate(FileValidate::class)
            ->scene('excel')
            ->check(['excel' => $uploadedFile]);

        $excelUtils = new ExcelUtils();
        $excelResult = $excelUtils->excel2arr('HotelKing', $uploadedFile);

        $mergeService = new MergeService();


        $order = $mergeService->hotelKing2order($excelResult);

        return $this->returnJson($order);
    }

    public function mergeGood(){
        $data = $this->request->param();
        
        return $this->returnJson($this->service->saveGood($data));

    }

    public function mergeConnecting(){
        $data = $this->request->param();
        
        return $this->returnJson($this->service->saveConnecting($data));

    }

    public function mergeReform(){
        $data = $this->request->param();
        
        return $this->returnJson($this->service->saveReform($data));

    }


    public function mergeGood2redis(){
        $key = $this->request->get('key','');

        $data = $this->request->post();
        validate(OrderValidate::class)
            ->scene('merge2redis')
            ->check(['key' => $key]);

        return $this->returnJson($this->service->saveGood2redis($key, $data));

    }

    public function mergeConnecting2redis(){
        $key = $this->request->get('key','');
        $data = $this->request->post();
        validate(OrderValidate::class)
            ->scene('merge2redis')
            ->check(['key' => $key]);
        return $this->returnJson($this->service->saveConnecting2redis($key, $data));

    }

    public function mergeReform2redis(){
        $key = $this->request->get('key','');
        $data = $this->request->post();
        validate(OrderValidate::class)
            ->scene('merge2redis')
            ->check(['key' => $key]);
        return $this->returnJson($this->service->saveReform2redis($key, $data));

    }



    public function download(){
        $param = $this->request->param();

       return $this->returnJson($this->service->toExcel($param));
    }

    public function downloadRedis(){
        $param = $this->request->param('key');

       return $this->returnJson($this->service->redis2Excel($param));
    }


}